package com.ruoyi.web.controller.apply;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.ApplyInfo;
import com.ruoyi.system.service.IApplyInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 制作申请Controller
 *
 * @author ess
 * @date 2021-10-25
 */
@Api("申请记录-1")
@RestController
@RequestMapping("/apply/info")
public class ApplyInfoController extends BaseController
{
    @Autowired
    private IApplyInfoService applyInfoService;

    /**
     * 查询制作申请列表
     * 我的申请-获取列表
     * 查询当前用户名下所有的申请
     * 已经通过审核的不能删除
     */
    @PreAuthorize("@ss.hasPermi('apply:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApplyInfo applyInfo)
    {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        applyInfo.setApplyUserId(user.getUserId().toString());
        List<ApplyInfo> list = applyInfoService.selectApplyInfoList(applyInfo);
        return getDataTable(list);
    }

    /**
     * 获取制作申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('apply:info:query')")
    @GetMapping(value = "/{applyInfoId}")
    public AjaxResult getInfo(@PathVariable("applyInfoId") String applyInfoId)
    {
        return AjaxResult.success(applyInfoService.selectApplyInfoByApplyInfoId(applyInfoId));
    }

    /**
     * 新增制作申请
     */
    @PreAuthorize("@ss.hasPermi('apply:info:add')")
    @Log(title = "制作申请", businessType = BusinessType.INSERT)
    @ApiOperation("新增制作申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyType", value = "申请类别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sealName", value = "用户名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "tempFile", value = "附件", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "jbrCardName", value = "经办人姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "jbrCardData", value = "经办人联系方式", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping
    public AjaxResult add(@RequestBody ApplyInfo applyInfo)
    {
        return toAjax(applyInfoService.insertApplyInfo(applyInfo));
    }

    /**
     * 修改制作申请
     */
    @PreAuthorize("@ss.hasPermi('apply:info:edit')")
    @Log(title = "制作申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApplyInfo applyInfo)
    {
        return toAjax(applyInfoService.updateApplyInfo(applyInfo));
    }

    /**
     * 删除制作申请
     */
    @PreAuthorize("@ss.hasPermi('apply:info:remove')")
    @Log(title = "制作申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applyInfoIds}")
    public AjaxResult remove(@PathVariable String[] applyInfoIds)
    {
        return toAjax(applyInfoService.deleteApplyInfoByApplyInfoIds(applyInfoIds));
    }

    /**
     * 文件上传
     */
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {
//        if (!file.isEmpty())
//        {
//            LoginUser loginUser = getLoginUser();
//            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
//            if (userService.updateUserAvatar(loginUser.getUsername(), avatar))
//            {
//                AjaxResult ajax = AjaxResult.success();
//                ajax.put("imgUrl", avatar);
//                // 更新缓存用户头像
//                loginUser.getUser().setAvatar(avatar);
//                tokenService.setLoginUser(loginUser);
//                return ajax;
//            }
//        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 导出制作申请列表
     */
    @PreAuthorize("@ss.hasPermi('apply:info:export')")
    @Log(title = "制作申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ApplyInfo applyInfo)
    {
        List<ApplyInfo> list = applyInfoService.selectApplyInfoList(applyInfo);
        ExcelUtil<ApplyInfo> util = new ExcelUtil<ApplyInfo>(ApplyInfo.class);
        return util.exportExcel(list, "制作申请数据");
    }

}
